#!/usr/bin/python
#
# Copyright (c) 2008, Mathias Weber
# Copyright (c) 2008, Thomas Stauffer
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are met:
#
# * Redistributions of source code must retain the above copyright notice, this
#   list of conditions and the following disclaimer.
#
# * Redistributions in binary form must reproduce the above copyright notice,
#   this list of conditions and the following disclaimer in the documentation
#   and/or other materials provided with the distribution.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
# DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE
# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
# SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
# CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
# OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#
# $Id$

"""
This filter tries to add a film grain effect. But IMO it is too irritating. Do
not know why exactly.
"""

import sys
import random

from pandac.PandaModules import Texture

from direct.filter.CommonFilters import CommonFilters
from direct.filter.CommonFilters import FilterManager
from direct.task import Task

import direct.directbase.DirectStart

"""
Set backgroundcolor before any filter or the background color on the texture is
not correct.
"""
base.setBackgroundColor(0.5, 0.5, 0.9)
base.disableMouse()

camera.setPos(0.0, -6.0, 3.0)
camera.lookAt(0.0, 0.0, 0.0)

"""
The following commented filter crashes on my computer if resolution is to high
(> 1400 x 1000).
"""
#filters = CommonFilters(base.win, base.cam)
#filters.setCartoonInk()
#filters.setBloom()

manager = FilterManager(base.win, base.cam)
tex = Texture("FilmGrain")
texNoise = loader.loadTexture("noise.png")
texNoise.setWrapU(Texture.WMRepeat)
texNoise.setWrapV(Texture.WMRepeat)
#quad = manager.renderSceneInto(colortex = tex) # bug in Panda3D 1.5.2
quad = manager.renderSceneInto(textures = {"color": tex})
quad.setShader(loader.loadShader("example-postprocess.sha"))
quad.setShaderInput("src", tex)
quad.setShaderInput("noisetex", texNoise)
quad.setShaderInput("noise", 1.0, 1.0, 1.0, 1.0)

def changeNoise(task):
    quad.setShaderInput("noise", random.random(), random.random(), 1.0, 1.0)
    return Task.cont

taskMgr.add(changeNoise, "Noise")

model = loader.loadModel("woman")
model.reparentTo(render)

base.accept("escape", sys.exit)
base.accept("a", render.analyze)
base.accept("l", render.ls)
base.accept("o", base.oobe)
base.accept("v", base.bufferViewer.toggleEnable)

run()
